package com.grant.code.controller;

import com.grant.code.Main;
import com.grant.code.exception.RegistrationException;
import com.grant.code.pojo.User;
import com.grant.code.service.UserService;
import com.grant.code.service.impl.UserServiceImpl;
import com.grant.code.utils.DialogUtils;
import com.grant.code.utils.SpringContextUtils;
import de.felixroske.jfxsupport.FXMLController;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.PasswordField;
import javafx.scene.control.TextField;
import javafx.stage.Stage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;

import java.io.IOException;
import java.net.URL;
import java.util.ResourceBundle;

@FXMLController
public class RegisterController implements Initializable {
    @FXML
    private TextField txtUsername;
    @FXML private PasswordField txtPassword;
    @FXML private PasswordField txtConfirmPwd;

    private UserService userService;

    @Override
    public void initialize(URL location, ResourceBundle resources) {
        ApplicationContext context = Main.getContext();
        userService = context.getBean(UserService.class);
        System.out.println(context);
    }

    @FXML
    private void onRegisterClick() {
        String username = txtUsername.getText().trim();
        String password = txtPassword.getText().trim();
        String confirmPwd = txtConfirmPwd.getText().trim();

        // 输入验证
        if (username.isEmpty() || password.isEmpty()) {
            DialogUtils.showErrorDialog("账号密码不能为空");
            return;
        }

        if (password.length() < 6) {
            DialogUtils.showErrorDialog("密码长度至少6位");
            return;
        }

        if (!password.equals(confirmPwd)) {
            DialogUtils.showErrorDialog("两次输入密码不一致");
            return;
        }
        if (userService.checkUserExist(username)) {
            DialogUtils.showErrorDialog("该账号已存在");
            return;
        }

        User user = new User(username, password);

        try {
            userService.register(user);
            DialogUtils.showSuccessDialog("注册成功", this::navigateToLogin);
        } catch (RegistrationException e) {
            DialogUtils.showErrorDialog(e.getMessage());
        }
    }

    // 返回登录页面
    @FXML
    private void navigateToLogin() {
        try {
            Stage stage = (Stage) txtUsername.getScene().getWindow();
            // 获取当前窗口的宽高
//            double currentWidth = stage.getWidth();
//            double currentHeight = stage.getHeight();
            Parent root = FXMLLoader.load(getClass().getResource("/fxml/login.fxml"));
            //Scene newScene = new Scene(root, currentWidth, currentHeight); // 显式设置宽高
            Scene newScene = new Scene(root);
            stage.setScene(newScene);
        } catch (IOException e) {
            e.printStackTrace();
            DialogUtils.showErrorDialog("无法返回登录页面");
        }
    }
}